var workId = T.p("workId");
var statusList = T.getDictItems("sys_status");
var subjectId = T.p("subjectId");
var isShowCond = T.p("isShowCond");
var returnParams = T.p("returnParams");
$(function () {
    $("#jqGrid").jqGrid({
        url: '../workItem/list',
        datatype: "json",
        postData: {workId: workId},
        colModel: [
            {label: 'id', name: 'id', width: 40, key: true},
            {label: '详情名称', name: 'itemName', width: 150},
            {
                label: '数据源From', name: 'dbKeyFrom', width: 50, formatter: function (value, options, row) {
                    return T.showDictDynaName("db_key", value);
                }
            },
            {
                label: '数据源To', name: 'dbKeyTo', width: 50, formatter: function (value, options, row) {
                    return T.showDictDynaName("db_key", value);
                }
            },
            {
                label: '类型', name: 'detailMode', width: 50, formatter: function (value, options, row) {
                    return T.showDictItemName("detail_mode", value);
                }
            },
            {
                label: '执行顺序', name: 'execNum', width: 50, align: "center", formatter: function (val, obj, row, act) {
                    var html = [];
                    html.push('<input type="hidden" name="ids"  value="' + row.id + '"/>');
                    html.push('<input name="sorts"  type="number" value="' + row.execNum + '" style="width:50px;height:19px;margin:0;padding:0;text-align:center;border:1px solid #ddd">');
                    return html.join('');
                }
            },
            {
                label: '状态', name: 'status', width: 50, formatter: function (value, options, row) {
                    return T.showDictItemName('sys_status', value);
                }
            },
            {
                label: '操作',
                name: 'actions',
                sortable: false,
                width: 80,
                formatter: function (value, options, row, act) {
                    var opts = [];
                    opts.push('<a href="javascript:" class="btnList" title="编辑作业项明细" onclick="vm.updateRow(' + row.id + ')" ><i class="fa fa-pencil"></i></a>&nbsp;');
                    if (row.status == 1) {
                        opts.push('<a href="javascript:" class="btnList" title="停用作业项明细" data-confirm="确认要停用该作业项明细吗？" data-click="vm.changeStatus(' + row.id + ',0)"><i class="glyphicon glyphicon-ban-circle"></i></a>&nbsp;');
                    }
                    if (row.status == 0) {
                        opts.push('<a href="javascript:" class="btnList" title="启用作业项明细" data-confirm="确认要启用该作业项明细吗？" data-click="vm.changeStatus(' + row.id + ',1)"><i class="glyphicon glyphicon-ok-circle"></i></a>&nbsp;');
                    }
                    return opts.join('');
                }
            }
        ],
        viewrecords: true,
        height: 800,
        rowNum: 20,
        rowList: [20, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        sortable: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.data",
            page: "page.pageNo",
            total: "page.pageCount",
            records: "page.count"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }, ondblClickRow: function (id) {
            location.href = "workItem_add.html?id=" + id;
        }, loadComplete: function () {
            btnMoreEventBind();
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        isShowCond: true,
        btnText: '隐藏',
        qryForm: {
            itemName: "",
            status: ""
        },
        editSort: {
            ids: [],
            sorts: []
        },
        workItem: {}
    },
    methods: {
        updateSort: function () {
            var ids = [];
            $("input[name='ids']").each(function () {
                ids.push($(this).val());
            })
            var sorts = [];
            $("input[name='sorts']").each(function () {
                sorts.push($(this).val());
            })
            this.editSort.ids = ids;
            this.editSort.sorts = sorts;
            $.ajax({
                type: "POST",
                url: "../workItem/updateSort",
                data: this.editSort,
                contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                traditional: true,
                dataType: "json",
                success: function (r) {
                    if (r.code == 0) {
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });

        },
        reset: function () {
            this.qryForm.itemName = "";
            this.qryForm.status = "";
        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'itemNameLike': '%' + vm.qryForm.itemName + '%',
                    status: vm.qryForm.status
                },
                page: 1
            }).trigger("reloadGrid");
        },
        toggle: function () {
            this.isShowCond = !this.isShowCond;

            if (this.isShowCond == true) {
                this.btnText = "隐藏"
            } else if (this.isShowCond == false) {
                this.btnText = "查询"
            }

        },
        create: function (event) {
            location.href = "workItem_add.html?workId=" + workId;
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }

            location.href = "workItem_add.html?id=" + id;
        },
        updateRow: function (id) {
            if (id == null) {
                return;
            }
            location.href = "workItem_add.html?id=" + id;
        },
        changeStatus: function (id, status) {
            var url = "../workItem/update";
            vm.workItem.id = id;
            vm.workItem.status = status;
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(vm.workItem),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            $("#jqGrid").trigger("reloadGrid");
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        back: function (event) {
            location.href = "/work/work.html?subjectId=" + subjectId + "&isShowCond=" + isShowCond + "&returnParams=" + encodeURI(returnParams);
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../workItem/delete",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        runOnce: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要立即执行选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../workItem/run",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        paramMgr: function () {
            location.href = "/work/workParams.html?belongType=2&belongId=" + workId;
        },
    }
});
